package com.example.shopping1902.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.shopping1902.me.UserBean;

import java.util.Map;

public class SaveUserUtils {
    // 将用户信息存储到共享参数当中
//   public static boolean saveUserInfo(Context context, Map<String,String>map){
//        SharedPreferences preferences = context.getSharedPreferences("user", context.MODE_PRIVATE);
//        SharedPreferences.Editor editor = preferences.edit();
//        for (Map.Entry<String,String>entry:map.entrySet()){
//            String key = entry.getKey();
//            String value = entry.getValue();
//            editor.putString(key,value);
//
//        }
//        editor.commit();
//        return true;
//    }
        private static UserBean.DataBean userinfo;

        public static UserBean.DataBean getUserinfo() {
            return userinfo;
    }
        public static void setUserinfo(UserBean.DataBean info){
            if (info!=null){
                SaveUserUtils.userinfo = info;
            }
        }
     //删除内存信息
        public static void delUserInfo(){
            userinfo = null;
        }
    //获取存储的用户信息
        public static UserBean.DataBean getUserToObj(Context context){
            SharedPreferences pref = context.getSharedPreferences("user", context.MODE_PRIVATE);
            int id = pref.getInt("id",-1);
            if (id!=-1) {
                String phone = pref.getString("phone", "");
                String password = pref.getString("password", "");
                String name = pref.getString("name", "");
                String address = pref.getString("address", "");
                String nickname = pref.getString("nickname", "");
                String question = pref.getString("question", "");
                String answer = pref.getString("answer", "");
                UserBean.DataBean bean = new UserBean.DataBean(id, phone, password, name, address, nickname, question, answer);
                setUserinfo(bean);
                return bean;
            }
            return null;

    }
    //保存用户信息到本地的方法
        public static void saveUserInfo(Context context,UserBean.DataBean bean){
            SharedPreferences pref = context.getSharedPreferences("user", context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putInt("id",bean.getId());
            editor.putString("phone",bean.getPhone());
            editor.putString("password",bean.getPassword());
            editor.putString("name",bean.getName());
            editor.putString("address",bean.getAddress());
            editor.putString("nickname",bean.getNickname());
            editor.putString("question",bean.getQuestion());
            editor.putString("answer",bean.getAnswer());
            editor.commit();
            setUserinfo(bean);

        }
        //删除个人信息
    public static void deleteUserInfo(Context context,UserBean.DataBean bean){
        SharedPreferences pref = context.getSharedPreferences("user", context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.remove("id");
        editor.remove("phone");
        editor.remove("password");
        editor.remove("name");
        editor.remove("address");
        editor.remove("nickname");
        editor.remove("question");
        editor.remove("answer");
        editor.commit();
    }
}
